<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>供应商列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/icon.css}">
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript">
        //工具栏
        var toolbar = [{
            text: '新增',
            iconCls: 'icon-edit',
            handler: function () {
                $('#dd').dialog({
                    title: "添加供应商"
                });
                $('#linkman,#suppliername,#phone').textbox('setValue', "");
                $('#addOrModify').val("add");
                $('#dd textarea').val("");
                $('#dd').dialog('open');
            }
        }, {
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                $('#dd').dialog({
                    title: "修改供应商信息"
                });
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择待修改的供应商");
                } else {
                    $('#addOrModify').val("modify");
                    $("#suppliername").textbox("setValue", selected.name);
                    $("#linkman").textbox("setValue", selected.linkman);
                    $("#phone").textbox("setValue", selected.phone);
                    $('#info').val(selected.info);
                    $('#id').val(selected.id);
                    $('#dd').dialog('open');
                }
            }
        }, '-', {
            text: '启用',
            iconCls: 'icon-ok',
            handler: function () {
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择要修改状态的用户");
                } else {
                    $.ajax({
                        url: "/manager/supplier/status/1",
                        data: {
                            "id": selected.id
                        },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.status !== 2001) {
                                alert(data.msg);
                            } else {
                                $('#booklist').datagrid('reload');
                            }
                        }
                    });
                }
            }
        }, '-', {
            text: '禁用',
            iconCls: 'icon-cancel',
            handler: function () {
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择要修改状态的用户");
                } else {
                    $.ajax({
                        url: "/manager/supplier/status/-1",
                        data: {
                            "id": selected.id
                        },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.status !== 2001) {
                                alert(data.msg);
                            } else {
                                $('#booklist').datagrid('reload');
                            }
                        }
                    });
                }
            }
        }];
        $(function () {
            $("#keyword").val("");
            $('#booklist').datagrid({
                title: '书籍列表',
                iconCls: 'icon-save',
                url: '/manager/supplier/findAll',
                columns: [
                    [
                        {field: 'id', hidden: true},
                        {field: 'info', hidden: true},
                        {field: 'name', title: '供应商名称', width: 100, align: 'center'},
                        {field: 'linkman', title: '联系人', width: 200, align: 'center'},
                        {field: 'phone', title: '联系电话', width: 150, align: 'center'},
                        {
                            field: 'flag', title: '是否启用', width: 80, align: 'center',
                            formatter: function (value, row, index) {
                                if (value === 1) {
                                    return "启用";
                                }
                                return "禁用";
                            }
                        },
                        {field: 'createtime', title: '入驻日期', width: 200, align: 'center'},
                        {
                            field: 'totalNumber', title: '累计进货数', width: 200, align: 'center',
                            formatter: function (value, row, index) {
                                if (value == null) {
                                    return "--";
                                }
                                return value;
                            }
                        },
                        {
                            field: 'totalSellnumber', title: '累计销售数', width: 200, align: 'center',
                            formatter: function (value, row, index) {
                                if (value == null) {
                                    return "--";
                                }
                                return value;
                            }
                        },
                        {
                            field: 'level', title: '等级', width: 200, align: 'center',
                            formatter: function (value, row, index) {
                                if (value == null) {
                                    return "--";
                                }
                                return value;
                            }
                        },
                    ]
                ],
                toolbar: toolbar,
                pagination: true,//表示在datagrid设置分页
                pageSize: 5,
                singleSelect: true,
                pageList: [5, 10, 20]
            });

            //初始化查询栏
            $("#submit_search").linkbutton({iconCls: 'icon-search', plain: true})
                .click(function () {
                    $('#booklist').datagrid({
                        queryParams: {
                            "suppliernameKey": $("#keyword").val(),
                        }
                    });   //点击搜索
                });

            //初始化添加修改窗口
            $('#dd').dialog({
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        let url = $("#addOrModify").val() === "add" ? "/manager/supplier/add" : "/manager/supplier/modify";
                        $.ajax({
                            url: url,
                            data: {
                                "suppliername": $("#suppliername").textbox("getValue"),
                                "linkman": $("#linkman").textbox("getValue"),
                                "phone": $("#phone").textbox("getValue"),
                                "info": $("#info").val(),
                                "id": $("#id").val(),
                            },
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                                if (data.status !== 2001) {
                                    $('#dd').dialog('close');
                                    alert(data.msg);
                                } else {
                                    $('#dd').dialog('close');
                                    $('#booklist').datagrid('reload');
                                }
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        alert('cancel');
                        ;
                    }
                }]
            });

        });
    </script>
</head>
<body>
<form name="searchform" method="post" action="" id="searchform">
    <td width="70" height="30"><strong>供应商查询：</strong></td>
    <td height="30">
        <input type="text" name="keyword" id="keyword" size=20 placeholder="供应商名称"/>
    </td>
    <td>
        <a id="submit_search">搜索</a>
    </td>
</form>
<table id="booklist"></table>
<div id="dd">

    <div align="center" style="margin-top: 30px">
        <form id="supplier" method="post">
            <input type="hidden" id="addOrModify"/>
            <input type="hidden" id="id" name="id"/>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="suppliername" id="suppliername" style="width:50%"
                       data-options="label:'供应商名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="linkman" id="linkman" style="width:50%"
                       data-options="label:'联系人:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="phone" id="phone" style="width:50%"
                       data-options="label:'联系电话:',required:true">
            </div>
            <table>
                <tr style="vertical-align:top">
                    <td>
                        公司简介:
                    </td>
                    <td>
                        <textarea rows="10" cols="30" id="info" name="info"></textarea>
                    </td>
                </tr>
            </table>

        </form>
    </div>
</div>
</body>
</html>